class Solution {
public:
    int lenLongestFibSubseq(vector<int>& arr) {
        int n = arr.size();

        // 下标绑定:
        unordered_map<int, int> hash;
        for(int i = 0; i < n; i++) hash[arr[i]] = i;

        vector<vector<int>> dp(n, vector(n, 2));
        int _max = 2;
        for(int j = 2; j < n; j++)
        {
            for(int i = 1; i < j; i++)
            {
                int a = arr[j] - arr[i];
                if(a < arr[i] && hash.count(a)) dp[i][j] = dp[hash[a]][i] + 1;
                _max = max(_max, dp[i][j]);
            }
        }
        return _max == 2 ? 0 : _max;
    }
};